//
//  YYMSearchViewController.m
//  On The Load
//
//  Created by 我爱的妞 on 15/11/16.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "YYMSearchViewController.h"
#import "AppTools.h"
#import "YMDTDetailsViewController.h"

@interface YYMSearchViewController () <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property (strong, nonatomic) UISearchBar *searchBar;

@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *dataArray;

@property (copy, nonatomic) NSString *key;

@end

@implementation YYMSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 80) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    [self.view addSubview:self.tableView];
    
    [self initSearchBar];
}

- (void)initSearchBar{
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 44)];
    self.searchBar.clearsContextBeforeDrawing = YES;
    self.searchBar.opaque = YES;
    self.searchBar.showsCancelButton = YES;
    self.searchBar.delegate = self;
    self.searchBar.placeholder = @"请输入关键字";
    
    [self.view addSubview:self.searchBar];
}

- (void)afnGetData{
    
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/suggestion?filter=city&latitude=38.882619&q=%@&longitude=121.539546&cityid=&_tk=1446642542:10892&apiVersion=1.1&clientVersion=1.2.1&idfa=850C72B5-29E6-464B-B861-D4895E1A9641", _key];
    NSString *urlEncode = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [AppTools getAFN:urlEncode andpara:nil andBlock:^(id result) {
        
        [_dataArray removeAllObjects];
        NSArray *array = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            
            [_dataArray addObject:dic];
        }
        
        [_tableView reloadData];
    } andfailBlock:^(id result) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    
    NSDictionary *dic = [_dataArray objectAtIndex:indexPath.row];
    
    NSString *str = [NSString stringWithFormat:@"%@-%@", [dic objectForKey:@"city"], [dic objectForKey:@"country"]];
    
    cell.textLabel.text = str;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dic = [_dataArray objectAtIndex:indexPath.row];
    
    YMDTDetailsViewController *vc = [[YMDTDetailsViewController alloc] init];
    
    vc.cityID = [dic objectForKey:@"id"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    [searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [self afnGetData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    _key = searchText;
}

@end
